/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.AsistanceControl;
import com.click.app.model.Employee;
import com.click.app.service.AsistanceControlManager;
import com.click.app.service.EmployeeManager;
import java.util.List;

/**
 *
 * @author ariel
 */
public class AsistanceControlAction extends BaseAction {

    private AsistanceControlManager asistanceControlManager;
    private EmployeeManager employeeManager;
    private AsistanceControl asistanceControl;
    private Employee employee;
    private Long idEmployee;
    private List<AsistanceControl> asistancesControl;

    public AsistanceControl getAsistanceControl() {
        return asistanceControl;
    }

    public void setAsistanceControl(AsistanceControl asistanceControl) {
        this.asistanceControl = asistanceControl;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setIdEmployee(Long idEmployee) {
        this.idEmployee = idEmployee;
    }

    public List<AsistanceControl> getAsistancesControl() {
        return asistancesControl;
    }

    public void setAsistanceControlManager(AsistanceControlManager asistanceControlManager) {
        this.asistanceControlManager = asistanceControlManager;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public String refreshAsistanceControlList() {
        System.out.println("refreshing asistances...");

        employee = (Employee) getSession().getAttribute("employee");
        asistancesControl = asistanceControlManager.findByEmployee(employee);

        return SUCCESS;
    }

    public String delete() {
        System.out.println("deleting Asistance: " + asistanceControl.getId() + "...");

        asistanceControlManager.remove(asistanceControl.getId());
        saveMessage(getText("asistanceControl.deleted"));

        employee = (Employee) getSession().getAttribute("employee");
        asistancesControl = asistanceControlManager.findByEmployee(employee);

        return SUCCESS;
    }

    public String save() {
        System.out.println("saving Asistance...");

        boolean isNew = (asistanceControl.getId() == null);
        
        employee = (Employee) getSession().getAttribute("employee");

        asistanceControl.setEmployee(employee);
        
        asistanceControlManager.save(asistanceControl);

        String key = (isNew) ? "asistanceControl.added" : "asistanceControl.updated";
        saveMessage(getText(key));

        asistancesControl = asistanceControlManager.findByEmployee(employee);

        return SUCCESS;
    }

    public String back() {
        getSession().removeAttribute("employee");
        return SUCCESS;
    }

    public String list() {
        if (idEmployee != null) {
            employee = employeeManager.get(idEmployee);
            getSession().setAttribute("employee", getEmployee());
        } else {
            employee = (Employee) getSession().getAttribute("employee");
        }

        if (employee != null) {
            asistancesControl = employeeManager.get(getEmployee().getId()).getAsistanceControls();
            return SUCCESS;
        } else {
            System.out.println("employee is null: idEmployee = " + idEmployee);
            return ERROR;
        }
    }

    public String help() {
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }
}
